// pages/studyPlan/studyPlan.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentYear: new Date().getFullYear(),
    currentMonth: new Date().getMonth() + 1,
    days: [],
    plans: [],
    showModal: false,
    newPlan: {
      date: '',
      title: '',
      content: ''
    }
  },
    // 生成日历
    generateCalendar() {
      const days = [];
      const date = new Date();
      const year = date.getFullYear();
      const month = date.getMonth();
      
      // 获取当月第一天
      const firstDay = new Date(year, month, 1);
      // 获取当月最后一天
      const lastDay = new Date(year, month + 1, 0);
      // 补全上月天数
      for (let i = 0; i < firstDay.getDay(); i++) {
        days.push({ day: '', isCurrentMonth: false });
      }
      // 填充当月天数
      for (let i = 1; i <= lastDay.getDate(); i++) {
        const currentDate = `${year}-${month + 1}-${i}`;
        days.push({
          day: i,
          date: currentDate,
          isCurrentMonth: true,
          hasPlan: this.checkHasPlan(currentDate)
        });
      }
      
      this.setData({ days });
    },
  
    // 加载本地计划
    loadPlans() {
      const plans = wx.getStorageSync('studyPlans') || [];
      this.setData({ plans });
    },
  
    // 检查是否有计划
    checkHasPlan(date) {
      return this.data.plans.some(plan => plan.date === date);
    },
  
    // 选择日期
    selectDate(e) {
      const date = e.currentTarget.dataset.date;
      this.setData({
        showModal: true,
        'newPlan.date': date
      });
    },
  
    // 输入处理
    inputTitle(e) {
      this.setData({ 'newPlan.title': e.detail.value });
    },
  
    inputContent(e) {
      this.setData({ 'newPlan.content': e.detail.value });
    },
  
    // 保存计划
    savePlan() {
      const { date, title, content } = this.data.newPlan;
      if (!title.trim()) {
        wx.showToast({ title: '请填写标题', icon: 'none' });
        return;
      }
  
      const newPlans = [...this.data.plans];
      const existingIndex = newPlans.findIndex(p => p.date === date);
      
      if (existingIndex > -1) {
        newPlans[existingIndex] = { date, title, content };
      } else {
        newPlans.push({ date, title, content });
      }
  
      wx.setStorageSync('studyPlans', newPlans);
      this.setData({
        plans: newPlans,
        showModal: false,
        newPlan: { date: '', title: '', content: '' }
      });
      this.generateCalendar();
    },
  
    // 关闭弹窗
    closeModal() {
      this.setData({
        showModal: false,
        newPlan: { date: '', title: '', content: '' }
      });
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadPlans();
    this.generateCalendar();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})